// pages/dingpiao/dingpiao.js
const db = wx.cloud.database()
  const app = getApp()
Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    movieId:'',
    cinemaId:'',
    cinemaName:'',
    place:'',
    poster:'',
    movieName:'',
    duration:'',
    type:'',
    people:'',
    dates:'',
    color:'',
    state: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },
  async onShow() {
    // 获取当前小程序的页面栈     
    let pages = getCurrentPages()
    // 数组中索引最大的页面--当前页面  
    let currentPage = pages[pages.length-1]
    var options = currentPage.options
    if (typeof(options.cinemaId) !== 'undefined') {
      var that = this
      db.collection('cinema').where({
        _id: options.cinemaId
      }).get().then(res => {
        wx.setNavigationBarTitle({
          title: res.data[0].funTitle
        });
        that.setData({
          cinemaName: res.data[0].funTitle,
          cinemaId: res.data[0]._id,
          place:res.data[0].place,
        });
      })
  }
  if (typeof(options.movieId) !== 'undefined') {
    var that = this
    db.collection('movies').where({
      _id: options.movieId
    }).get().then(res => {
      that.setData({
        poster: res.data[0].poster,
        movieId: res.data[0]._id,
        movieName:res.data[0].title,
        duration:res.data[0].duration,
        type:res.data[0].type,
        people:res.data[0].people
      });
    })
    const app = getApp()
      var movieId = options.movieId
      var cinemaId = options.cinemaId
      var sessions = await app.getTimeRightSession1(movieId,cinemaId)
      var dates = sessions.map(session_info => session_info.date); 

      this.setData({
        movieId:movieId,
        cinemaId:cinemaId,
        dates:Array.from(new Set(dates))
      })
      
      var date = this.data.dates[this.data.state]
      var sessions = await app.getAppointTimeSession(movieId,cinemaId,date)
      this.setData({
        sessions:sessions
      })

    }     
},
sessionShow:async function (event) {
  const cinemaId=this.data.cinemaId;
  const movieId=this.data.movieId;
  const date = event.currentTarget.dataset.dates;
  var sessions =  await app.getAppointTimeSession(movieId,cinemaId,date)
  this.setData({
    sessions:sessions,
    state:event.currentTarget.dataset.key
  })
},
onMovieTap:function(event){
  const sessionId = event.currentTarget.dataset.id;
  wx.navigateTo({
    url: `/pages/selection/selection?sessionId=${sessionId}`
  })
}
})